// 表单验证
import WxValidate from '../../utils/WxValidate.js'

// 引入请求api
import {
  Login
} from "../../config/api.js"

// 引入请求函数
import _util from "../../utils/util.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    idcard: '',
    password: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate() //验证规则函数
  },

  //报错 
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },

  //验证函数
  initValidate() {
    const rules = {
      idcard: {
        required: true,
        idcard: true,
      },
      password: {
        required: true
      }
    }
    const messages = {
      idcard: {
        required: '请输入身份证号码',
      },
      password: {
        required: '请输入密码'
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },

  submit(e) {
    wx.showLoading({
      title: '登录中',
    })
    let that = this;
    const params = e.detail.value
    console.log(params)
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false
    };
    
    _util.apiRequest(Login, params).then( r => {
      console.log('rdl',r)
      if(r.data.code == 0){
        that.showModal({msg:r.data.info})
      } else if (r.data.code == 1){
        wx.setStorageSync('member', r.data.data);
        wx.hideLoading()
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }

})